package edu.pku.sei.transformation.editor.views.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.modeler.figures.CommonFigure;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class RuleFigure extends CommonFigure {

	protected Color foregroundColor = ColorConstants.black;
	protected Color backgroundColor = ColorConstants.white;
	protected Figure nameArea = new Figure();
	private Label name = new Label("test");
	private Label upperLabel = new Label("<<Rule>>");
	private Label lowerLabel = new Label("");

	public RuleFigure() {
		name.setFont(FontConstant.TITLE_FONT);
		upperLabel.setFont(FontConstant.STEREO_TYPE_LABEL);
		// lowerLabel.setFont(FontConstant.ARIAL_7);
		//name.setIcon(IconsProvider.getImage(IconsProvider.RULE_ICO));
		nameArea.setLayoutManager(new ToolbarLayout());
		nameArea.add(upperLabel);
		nameArea.add(name);
		// nameArea.add(lowerLabel);
	}

	protected void paintFigure(Graphics graphics) {
		graphics.pushState();

		// graphics.fillGradient(getBounds(), true);
		Rectangle rect = getBounds().getCopy();
		graphics.translate(getLocation());
		int width = rect.width;
		int height = rect.height;
		int cornerSize = 5;
		// name.setLocation(new Point(0,0));
		// fill the note
		PointList outline = new PointList();
		outline.addPoint(0, height / 2);
		outline.addPoint(width / 8, 0);
		outline.addPoint(7 * width / 8, 0);
		outline.addPoint(width, height / 2);
		outline.addPoint(7 * width / 8, height);
		outline.addPoint(width / 8, height);

		graphics.setBackgroundColor(this.backgroundColor);
		graphics.setForegroundColor(this.foregroundColor);
		graphics.setLineWidth(2);
		// graphics.fillPolygon(outline);
		graphics.drawPolygon(outline);
		// graphics.drawPolyline(outline);

		graphics.translate(getLocation().getNegated());
		graphics.popState();
	}

	public void setName(String newName) {
		this.name.setText(newName);
	}

	public void setUpperLabel(String s) {
		this.upperLabel.setText(s);
	}

	public void setLowerLabel(String s) {
		this.lowerLabel.setText(s);
	}

	public void hightLight(boolean isHighLight) {
		if (isHighLight)
			foregroundColor = ColorConstants.red;
		else
			foregroundColor = ColorConstants.black;
		this.repaint();
	}

}
